﻿using B.S.DataBase.MyProject.Domain.productionworkorder;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SCWorkOrderCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SCWorkOrderCommandHandler
{
    /// <summary>
    /// 查询生产工单的详情页面命令
    /// </summary>
    public class SCWorkOrderXQQueryCommandHandler:IRequestHandler<SCWorkOrderXQQueryCommand,APIResult<List<SCWorkOrderXQDtos>>>
    {
        /// <summary>
        /// 基本信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo;
        /// <summary>
        /// 物品信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionItem> wRepo;
        /// <summary>
        /// 工序流程仓储接口
        /// </summary>
        private readonly IBaseRepository<ProcessFlowSheet> lRepo;
        /// <summary>
        /// 物品名称仓储接口
        /// </summary>
        private readonly IBaseRepository<item_classification_detail> wnRepo;
        /// <summary>
        /// 单号仓储接口
        /// </summary>
        private readonly IBaseRepository<AssociatedProductionOrder> dhRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="jRepo"></param>
        public SCWorkOrderXQQueryCommandHandler(IBaseRepository<ProductionWorkOrderBasicInfoSheet>jRepo,IBaseRepository<ProductionItem>wRepo,IBaseRepository<ProcessFlowSheet>lRepo,IBaseRepository<item_classification_detail>wnRepo,IBaseRepository<AssociatedProductionOrder>dhRepo)
        {
            this.jRepo = jRepo;
            this.wRepo = wRepo;
            this.lRepo = lRepo;
            this.wnRepo = wnRepo;
            this.dhRepo = dhRepo;
        }

        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<SCWorkOrderXQDtos>>> Handle(SCWorkOrderXQQueryCommand request, CancellationToken cancellationToken)
        {
            var list = from a in jRepo.GetAll().Result
                       join b in wRepo.GetAll().Result on a.ProductionItemId equals b.ProductionItemId
                       join c in wnRepo.GetAll().Result on b.itemName equals c.item_classification_detailID
                       join d in lRepo.GetAll().Result on a.ProcessFlowSheetId equals d.ProcessFlowSheetId
                       join e in dhRepo.GetAll().Result on a.AssociatedProductionOrderNumber equals e.AssociatedProductionOrderId
                       select new SCWorkOrderXQDtos
                       {
                           WorkOrderId = a.WorkOrderId,
                           WorkOrderName = a.WorkOrderName,
                           AssociatedProductionOrderNumber = a.AssociatedProductionOrderNumber,
                           SalesOrder = a.SalesOrder,
                           AssociatedProject = a.AssociatedProject,
                           WorkOrderDate = a.WorkOrderDate,
                           PersoninCharge = a.PersoninCharge,
                           AffiliatedDepartment = a.AffiliatedDepartment,
                           Remarks = a.Remarks,
                           WenJian = a.WenJian,
                           itemName = b.itemName,
                           itemNumber = b.itemNumber,
                           specification = b.specification,
                           unit = b.unit,
                           itemCategory = b.itemCategory,
                           itemClassification = b.itemClassification,
                           plannedStartTime = b.plannedStartTime,
                           plannedEndTime = b.plannedEndTime,
                           plannedOutput = b.plannedOutput,
                           ProcessFlowSheetId = d.ProcessFlowSheetId,
                           ProcessCode = d.ProcessCode,
                           ProcessName = d.ProcessName,
                           ProcessingOrder = d.ProcessingOrder,
                           ProcessFlow = d.ProcessFlow,
                           PesponsiblePerson = d.PesponsiblePerson,
                           Pepartment = d.Pepartment,
                           PlannedStartDate = d.PlannedStartDate,
                           PlannedEndDate = d.PlannedEndDate,
                           PlannedQuantity = d.PlannedQuantity,
                           SCDHao = e.AssociatedProductionName,
                           SCWName = c.item_name

                       };
            if (request.WorkOrderId!=null)
            {
                list = list.Where(x => x.WorkOrderId == request.WorkOrderId);
            }
            APIResult<List<SCWorkOrderXQDtos>> result = new APIResult<List<SCWorkOrderXQDtos>>();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            result.Count = list.Count();
            return await Task.FromResult(result);
        }

    }

}
